// ==UserScript==
// @name         I don't want to see you
// @namespace    http://your.homepage/
// @version      0.1
// @description  enter something useful
// @author       You
// @match        http*://boards.na.leagueoflegends.com/*
// @run-at document-end
// @grant       GM_getValue
// @grant       GM_setValue
// ==/UserScript==
/*
 *  Set up our Variables. This could be turned into a "app" but I prefer old school c syntaxical sugar not that new fangle mamsy stuff
 *
 */
var $$; //polite reference to jquery
var bannedlist = [];
var superban = []; // don't even want to know they exist
var pagecomments;
setTimeout(FindJquery, 300); //neat trick to push yourself on the back of the script stack. You know, because userscripts.


function Main()
{


    ReadBannList();
    MuteOffenders();
    ReadSuperBannList();
    DisperseUsers();
    pagecomments = $$('.nested-comment');
    pagecomments .each(FilterUserComments);

}

/**
 * Checks to see if the jquery script has been loaded -- used after a settimeout to push it back on the stack (better hit rate)
 * @method FindJquery
 * @return
 */
function FindJquery() {

    if (typeof $ == 'function') {
        console.log('jquery started');
        $$ = $;
        Main();

    }
}

/**
 * Clarity function that called the each state ment for the superban list. May add additional logic later so refractor now
 * @method DisperseUsers
 * @return
 */
function DisperseUsers()
{
    $(superban).each(DisperseUserOnPage);
}
/**
 * This is the callback for the disperse users function. It goes through each nested comment that has an img with the blocked user and vanishes it from the page. kame hame ha style
 * @method DisperseUserOnPage
 * @param {} i    index in list
 * @param {} img  userid
 * @return
 */
function DisperseUserOnPage(i,img)
{
    $$('img[src="' + img + '"]').parents('.nested-comment').hide();
}

/**
 * This function goes through the banned list (showing a for loop this time) and hides the body of their comment. You can still unmute them at this point.
 * @method MuteOffenders
 * @return
 */
function MuteOffenders() {
    console.log(bannedlist);

    for(var i=0;i<bannedlist.length;i++)
    {
        var img = bannedlist[i];
        console.log(img);
        $$('img[src="' + img + '"]').parents('.nested-comment').each(HideComment);

    }
}




/**
 * This runs over each commentbox and adds the new user commands.
 * @method FilterUserComments
 * @param {} index
 * @param {} domitem
 * @return
 */
function FilterUserComments(index, domitem) {
    // var hover =    box.find('a[data-apollo-widget]');

    var box = $$(domitem);
    var userid = box.find('.icon img').attr('src'); //images have to be unique, names do not

    if (userid in bannedlist) {

    } else if (box.attr('muted')) {
        box.find('.controls').prepend(
            $$("<li class='MuteButton' />").append($$("<a />").text("Mute Person").attr("href", "javascript:void(0)").attr('MuteButton',1).click(AddPersonToBannList)).hide());
        box.find('.controls').prepend(
            $$("<li class='UnMuteButton' />").append($$("<a />").text("UnMute Person").attr("href", "javascript:void(0)").click(RemovePersonFromBannList)).attr('id',userid));
    } else {
        box.find('.controls').prepend(
            $$("<li class='MuteButton' />").append($$("<a />").text("Mute Person").attr("href", "javascript:void(0)").attr('MuteButton',1).click(AddPersonToBannList)));
        box.find('.controls').prepend(
            $$("<li class='UnMuteButton' />").append($$("<a />").text("UnMute Person").attr("href", "javascript:void(0)").click(RemovePersonFromBannList)).attr('id',userid).hide());
    }
    box.find('.controls').prepend(
        $$("<li />").append($$("<a />").text("Disperse Person").attr("href", "javascript:void(0)").click(AddtoSuperList)));
}
/**
 * Hides the ppl we don't like on the page
 * @method HideComment
 * @param {} i
 * @param {} dom
 * @return
 */
function HideComment(i, dom) {

    var commentbox = $$(dom);
    commentbox.attr('muted', true);
    commentbox.find('.body').hide();

    commentbox.find('.MuteButton').hide();
    commentbox.find('.UnMuteButton').show();

}

/**
 * Adds a person to the super (dispersed) user list
 * @method AddtoSuperList
 * @param {} e
 * @return
 */
function AddtoSuperList(e) {

    var ele = $$(e.target);
    var box = ele.parents('.nested-comment').hide();
    var userid = box.find('.icon img').attr('src'); //images have to be unique, names do not
    if (!(userid in superban))
        superban.push(userid);

    SaveSuperBannedList();
}

/**
 * Adds a person to the mute list
 * @method AddPersonToBannList
 * @param {} e
 * @return
 */
function AddPersonToBannList(e) {

    var ele = $$(e.target);



    var box = ele.parents('.nested-comment');

    var userid = box.find('.icon img').attr('src'); //images have to be unique, names do not
    if (!(userid in bannedlist))
        bannedlist.push(userid);
    else
        return;

    var userscommentboxes = $$('img[src="' + userid + '"]').parents('.nested-comment');
    userscommentboxes.find('.body').hide();
    userscommentboxes.find('.MuteButton').hide();
    userscommentboxes.find('.UnMuteButton').show();

    SaveBannedList();
}
/**
 * Wrapper function not needed but makes code cleaner. The setTimeout randomly is bad for code clarity. JS parser strips the function anyway.
 * @method SaveBannedList
 * @return
 */
function SaveBannedList() {

    setTimeout(SaveBannedList_wrapper, 0); //needed to access GM_
}

/**
 * now that we have proper scope, let's save that value
 * @method SaveBannedList_wrapper
 * @return
 */
function SaveBannedList_wrapper() {
    console.log(JSON.stringify(bannedlist));
    GM_setValue("banned", JSON.stringify(bannedlist));
}

/**
 * Halp I needz some scope
 * @method SaveSuperBannedList
 * @return
 */
function SaveSuperBannedList() {

    setTimeout(SaveSuperBannedList_wrapper, 0); //needed to access GM_
}

/**
 * Dr. Scope, paging dr. scope
 * @method SaveSuperBannedList_wrapper
 * @return
 */
function SaveSuperBannedList_wrapper() {
    GM_setValue("superbanned", JSON.stringify(superban));
}

/**
 * Read the ban list or create a new one. We could just check for undefined but why not show how to trap an error?
 * @method ReadBannList
 * @return
 */
function ReadBannList() {
    console.log("reading banned");
    var tmp = GM_getValue("banned");
    try {
        console.log(tmp);
        bannedlist = $$.parseJSON(tmp);

        console.log("banned loaded: " + bannedlist.length);
    } catch (e) {
        console.log("error is " + e);
        bannedlist = [];

    }

}

/**
 * read the super ban list
 * @method ReadSuperBannList
 * @return
 */
function ReadSuperBannList() {

    var tmp = GM_getValue("superbanned");
    try {
        superban = $$.parseJSON(tmp);
    } catch (e) {
        superban = [];

    }

}

/**
 * You have been granted amnesty
 * @method RemovePersonFromBannList
 * @param {} e
 * @return
 */
function RemovePersonFromBannList(e) {

    var ele = $$(e.target);
    var person = ele.closest('li').attr('id');

    for (var i = 0; i < bannedlist.length; i++) {
        if (bannedlist[i] == person) {
            bannedlist.splice(i, 1);
            break;;
        }
    }
    var userscommentboxes = $$('img[src="' + person + '"]').parents('.nested-comment');
    userscommentboxes.find('.body').show();
    userscommentboxes.find('.MuteButton').show();
    userscommentboxes.find('.UnMuteButton').hide();

    SaveBannedList();
}

//$('.username').each(FilterUserComments) //parents('.nested-comment')
